﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using API.Models;
using API.Models.Repository;

namespace API.Controllers.api.v1
{
    [API.Core.AuthorizeAttribute.EdexAuthorizeAttribute]
    public class BasicController : ApiController
    {
        public SEC_SESSIONS currentSession
        {
            get
            {
                return (SEC_SESSIONS)this.ControllerContext.RouteData.Values["session"];
            }
        }

        public BasicController()
        { 
            var sessionRepository = new SEC_SESSIONS_Repository();
        }
    }

    public class CommonController<T, R> : BasicController
        where T : class, ICommonDbClass
        where R : IGenericRepository<T>, new()
    {
        
        // GET /api/wh_companies
        [Queryable]
        public virtual IQueryable<T> Get()
        {
            var repository = new R();
            repository.SetSession(this.currentSession);

            var data = repository.Find().AsQueryable();
            return data;
        }

        // GET /api/wh_companies/5
        public virtual T Get(Guid id)
        {
            var repository = new R();
            repository.SetSession(this.currentSession);

            var data = repository.Find(r => r.GUID_RECORD == id);
            return data.FirstOrDefault();
        }

        // POST /api/wh_companies
        [HttpPost]
        public virtual void Post(T value)
        {
            var repository = new R();
            repository.SetSession(this.currentSession);

            value.GUID_RECORD = Guid.NewGuid();

            repository.Add(value);
            repository.Save();
        }

        // PUT /api/wh_companies/5
        [HttpPut]
        public virtual void Put(T value)
        {
            var repository = new R();
            repository.SetSession(this.currentSession);

            repository.Edit(value);
            repository.Save();
        }

        // DELETE /api/wh_companies/5
        [HttpDelete]
        public virtual void Delete(Guid id)
        {
            var repository = new R();
            repository.SetSession(this.currentSession);

            var entity = repository.Find(r => r.GUID_RECORD == id).FirstOrDefault();
            repository.Delete(entity);
            repository.Save();
        }
    }

    public class CommonController<T> : BasicController where T : class, ICommonDbClass
    {

        // GET /api/wh_companies
        [Queryable]
        public virtual IQueryable<T> Get()
        {
            var repository = RepositoryFactory<T>.Create();
            repository.SetSession(this.currentSession);

            //return repository.Find().AsQueryable();

            var data = repository.Find().AsQueryable();
            return data;
        }

        // GET /api/wh_companies/5
        public virtual T Get(Guid id)
        {
            var repository = RepositoryFactory<T>.Create();
            repository.SetSession(this.currentSession);

            var data = repository.Find(r => r.GUID_RECORD == id);
            return data.FirstOrDefault();
        }

        // POST /api/wh_companies
        [HttpPost]
        public virtual void Post(T value)
        {

            if (value.GUID_RECORD != null && 
                value.GUID_RECORD != Guid.Empty)
            {
                this.Put(value);
                return;
            }

            var repository = RepositoryFactory<T>.Create();
            repository.SetSession(this.currentSession);

            value.GUID_RECORD = Guid.NewGuid();

            repository.Add(value);
            repository.Save();
        }

        // PUT /api/wh_companies/5
        [HttpPut]
        public virtual void Put(T value)
        {
            var repository = RepositoryFactory<T>.Create();
            repository.SetSession(this.currentSession);

            repository.Edit(value);
            repository.Save();
        }

        // DELETE /api/wh_companies/5
        [HttpDelete]
        public virtual void Delete(Guid id)
        {
            var repository = RepositoryFactory<T>.Create();
            repository.SetSession(this.currentSession);

            var entity = repository.Find(r => r.GUID_RECORD == id).FirstOrDefault();
            repository.Delete(entity);
            repository.Save();
        }
    }
}
